import java.util.*;
class RemoveAnagrams {
    public List<String> removeAnagrams(String[] words) {
        if(words == null || words.length == 0) {
            return new ArrayList<>();
        }
        char[] chars = words[0].toCharArray();
        Arrays.sort(chars);
        String base = new String(chars);
        List<String> result = new ArrayList<>();
        result.add(words[0]);
        for (int i = 1; i < words.length; i++) {
            String word = words[i];
            char[] chs = words[i].toCharArray();
            Arrays.sort(chs);
            if(!new String(chs).equals(base)) {
                result.add(word);
                base = new String(chs);
            }
        }
        return result;
    }
    public static void main(String[] args) {
        String[] words = {"abba","baba","bbaa","cd","cd"};
        RemoveAnagrams res = new RemoveAnagrams();
        List<String> result = res.removeAnagrams(words);
        System.out.println(result);
    }
}